package ua.org.nuos.configuration;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Stateless;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 23.10.11
 * Time: 17:03
 * To change this template use File | Settings | File Templates.
 */
@Local
@Stateless
public class SdmsConfiguration {

    protected static final String CONFIGURATION_FILE = "/sdms.conf";

    protected Properties properties;

    @PostConstruct
    protected void initProperties() {
        try {
            if (properties != null) {
                throw new RuntimeException("SdmsConfiguration can not be initialized more then one");
            }
            properties = new Properties();
            InputStream is = SdmsConfiguration.class.getResourceAsStream(CONFIGURATION_FILE);
            properties.load(is);
        } catch (Throwable ex) {
            throw new RuntimeException("Load configuration problems", ex);
        }
    }

    protected String getProperty(String name) {
        return properties.getProperty(name);
    }

    public String getClientGuiHost() {
        return getProperty("clientgui_host");
    }

    public Properties getGmailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", getProperty("mail.smtp.submitter"));
        properties.setProperty("mail.transport.protocol", getProperty("mail.transport.protocol"));
        properties.setProperty("mail.smtp.auth", getProperty("mail.smtp.auth"));
        properties.setProperty("mail.smtp.host", getProperty("mail.smtp.host"));
        properties.setProperty("mail.smtp.port", getProperty("mail.smtp.port"));
        properties.setProperty("mail.smtp.socketFactory.class", getProperty("mail.smtp.socketFactory.class"));
        properties.setProperty("mail.smtp.socketFactory.fallback", getProperty("mail.smtp.socketFactory.fallback"));
        properties.setProperty("mail.smtp.quitwait", getProperty("mail.smtp.quitwait"));
        return properties;
    }

    public String getGmailSenderUser() {
        return getProperty("mail_user");
    }

    public String getGmailSenderPassword() {
        return getProperty("mail_password");
    }

}
